package com.zslh.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.zslh.entity.Message;
import com.zslh.util.DBHelp;
import com.zslh.util.RowMapper;

public class MessageDao {
	DBHelp<Message> db=new DBHelp<Message>();
	
	public void save(Message msg){
		String sql="INSERT INTO t_message(messageId,username,nowTime,message) VALUES(?,?,?,?)";
		db.executeSQL(sql, msg.getMessageId(),msg.getUsername(),msg.getNowTime(),msg.getMessage());
	}



	public List<Message> findAll() {
		String sql = "select messageId,username,nowTime,message from t_message";
		return db.executeQueryForList(sql, new MessageRowMapper());
	}
	public class MessageRowMapper implements RowMapper<Message>{

		public Message mapperRow(ResultSet rs) throws SQLException {
			Message msg=new Message();
			msg.setMessage(rs.getString("message"));
			msg.setMessageId(rs.getString("messageId"));
			msg.setNowTime(rs.getString("nowTime"));
			msg.setUsername(rs.getString("username"));
			return msg;
		}
	
	}
}